﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class admin_qlfeedbacks : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            string connStr = ConfigurationManager.ConnectionStrings["canhbuomxanhconnectionstring"].ConnectionString;
            SqlConnection mySQLconnection = new SqlConnection(connStr);
            if (mySQLconnection.State == ConnectionState.Closed)
            {
                mySQLconnection.Open();
            }
            SqlCommand mySqlSelect = new SqlCommand("select * from Feedbacks", mySQLconnection);

            mySqlSelect.CommandType = CommandType.Text;
            SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlSelect);
            DataSet myDataSet = new DataSet();
            mySqlAdapter.Fill(myDataSet);
            Repeater1.DataSource = myDataSet;
            Repeater1.DataBind();
            mySQLconnection.Close();
        }
    }

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.Equals("del"))
        {
            Response.Write("ID delete is: " + e.CommandArgument.ToString());
            this.delete(Convert.ToInt32(e.CommandArgument.ToString()));
        }
        Response.Redirect(Request.Url.ToString());
    }

    protected void Repeater1_ItemCreated1(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    {
        RepeaterItem ri = (RepeaterItem)e.Item;

        if (ri.ItemType == ListItemType.Item || ri.ItemType == ListItemType.AlternatingItem)
        {
            CheckBox cb = ri.FindControl("cb") as CheckBox;
            cb.CheckedChanged += new EventHandler(CheckedChanged);
        }
    }

    private void CheckedChanged(object sender, EventArgs e)
    {
        CheckBox cb = (CheckBox)sender;
        /*
         * Update data
         * 
         */
        this.updateCheck(Convert.ToInt32(cb.ToolTip.ToString()), cb.Checked);

    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ((CheckBox)e.Item.FindControl("cb")).Attributes.Add("rid", ((DataRowView)e.Item.DataItem)["id"].ToString());
        }
    }

    private void delete(Int32 id)
    {
        connection connect = new connection();
        SqlConnection conn = connect.connect();
        String sql = "delete from [Feedbacks] where [id] = @id";
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = sql;
        cmd.Connection = conn;
        cmd.Parameters.AddWithValue("id", id);
        cmd.ExecuteNonQuery();
    }
    private void updateCheck(Int32 id, bool check)
    {
        connection connect = new connection();
        SqlConnection conn = connect.connect();

        String sql = "update [Feedbacks] set [check] = @check where [id] = @id";
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = sql;
        cmd.Connection = conn;
        cmd.Parameters.AddWithValue("check", check);
        cmd.Parameters.AddWithValue("id", id);
        cmd.ExecuteNonQuery();
    }
}